package org.jsrml.core.supplier.service;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.core.supplier.command.RemoveContactCo;
import org.jsrml.core.supplier.command.SaveContactCo;
import org.jsrml.core.supplier.entity.Contact;
import org.jsrml.core.supplier.entity.Vendor;
import org.jsrml.core.supplier.exception.SupplierException;
import org.jsrml.core.supplier.qo.ContactQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ContactService extends BaseDao<Contact, ContactQO> {

	@Autowired
	private VendorService vendorService;
	
	public Contact create(SaveContactCo command) throws SupplierException{
		
		if(StringUtils.isBlank(command.getVendorId())){
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商id不能为空！");
		}
		
		Vendor vendor = vendorService.get(command.getVendorId());
		if(vendor==null){
			throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
		}
		Contact contact = new Contact();
		contact.create(command, vendor);
		save(contact);
		return contact;
	}
	
	public Contact modify(SaveContactCo command) throws SupplierException{
		
		if(StringUtils.isBlank(command.getVendorId())){
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商id不能为空！");
		}
		
		if(StringUtils.isBlank(command.getContactId())){
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商联系人id不能为空！");
		}
		
		Vendor vendor = vendorService.get(command.getVendorId());
		if(vendor==null){
			throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
		}
		Contact contact =get(command.getContactId());
		
		if(contact==null){
			throw new SupplierException(SupplierException.CONTACT_NOT_FOUND, "供应商联系人没有找到！");
		}
		
		contact.modify(command, vendor);
		save(contact);
		return contact;
	}
	
	public void remove(RemoveContactCo command) throws SupplierException{
		if(StringUtils.isBlank(command.getContactId())){
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商联系人id不能为空！");
		}
		Contact contact =get(command.getContactId());
		if(contact==null){
			throw new SupplierException(SupplierException.CONTACT_NOT_FOUND, "供应商联系人没有找到！");
		}
		contact.remove();
		save(contact);
	}
	
	@Transactional(rollbackFor=SupplierException.class)
	public void plRemove(RemoveContactCo command) throws SupplierException{
		if(command.getContactIds()!=null){
			for(String id : command.getContactIds()){
				Contact contact =get(id);
				if(contact==null){
					throw new SupplierException(SupplierException.CONTACT_NOT_FOUND, "供应商联系人没有找到！");
				}
				contact.remove();
				save(contact);
			}
		}
	}
	
	public void close(String contactId) throws SupplierException{
		Contact contact =get(contactId);
		if(contact==null){
			throw new SupplierException(SupplierException.CONTACT_NOT_FOUND, "供应商联系人没有找到！");
		}
		contact.close(null);
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, ContactQO qo) {
		return criteria;
	}

	@Override
	protected Class<Contact> getEntityClass() {
		return Contact.class;
	}
	
}
